package com.chen.authority.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.chen.authority.common.constant.SysConstant;
import com.chen.authority.common.response.MultiResponse;
import com.chen.authority.model.vo.OnlineUserVo;
import com.chen.authority.model.vo.UserVo;
import com.chen.authority.service.OnlineUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class OnlineUserServiceImpl implements OnlineUserService {

    @Override
    public MultiResponse<OnlineUserVo> findOnlineUserList(String username, String name) {
        List<OnlineUserVo> onlineUserVoList = new ArrayList<>();
        List<String> keys = StpUtil.searchTokenValue(StrUtil.EMPTY, -1, 0);
        for (String key : keys) {
            String token = key.replace(SysConstant.LOGIN_TOKEN_KEY, StrUtil.EMPTY);
            if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < 0) {
                StpUtil.kickoutByTokenValue(token);
                continue;
            }

            UserVo userVo = (UserVo) StpUtil.getTokenSessionByToken(token).get(SysConstant.LOGIN_USER);
            OnlineUserVo onlineUserVo = BeanUtil.toBean(userVo, OnlineUserVo.class);
            onlineUserVo.setOnlineTime(DateUtil.formatBetween(DateUtil.parse(DateUtil.now()),
                    userVo.getLoginTime(), BetweenFormatter.Level.MINUTE));
            onlineUserVoList.add(onlineUserVo);
        }

        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(name)) {
            onlineUserVoList = onlineUserVoList.stream()
                    .filter(onlineUserVo -> onlineUserVo.getUsername().contains(username)
                            && onlineUserVo.getName().contains(name)).collect(Collectors.toList());
        } else if (StringUtils.isNotBlank(username)) {
            onlineUserVoList = onlineUserVoList.stream()
                    .filter(onlineUserVo -> onlineUserVo.getUsername().contains(username)).collect(Collectors.toList());
        } else if (StringUtils.isNotBlank(name)) {
            onlineUserVoList = onlineUserVoList.stream()
                    .filter(onlineUserVo -> onlineUserVo.getName().contains(name)).collect(Collectors.toList());
        }

        return MultiResponse.success(onlineUserVoList);
    }

    @Override
    public long getOnlineUserCount() {
        long onlineUserCount = 0;
        List<String> keys = StpUtil.searchTokenValue(StrUtil.EMPTY, -1, 0);
        for (String key : keys) {
            String token = key.replace(SysConstant.LOGIN_TOKEN_KEY, StrUtil.EMPTY);
            if (StpUtil.stpLogic.getTokenActivityTimeoutByToken(token) < 0) {
                continue;
            }
            onlineUserCount += 1;
        }
        return onlineUserCount;
    }

}

